// vue2 路由配置
// import VueRouter from 'vue-router'
// const router  = new VueRouter({
//   mode: 'history' // hash history,
//   routes: [
//     { path: '/home', component: () => import('@/views/Home/index.vue')}
//   ]
// })
// export default router

// vue3 路由配置
import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'
// 引入 NProgress 及其样式
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// 关闭 NProgress 的 loading 效果
NProgress.configure({
  showSpinner: false
})
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  // history: createWebHashHistory(),
  routes: [
    // 登陆页面
    { path: '/login', component: () => import('@/views/Login/index.vue') },
    // 布局容器
    {
      path: '/',
      component: () => import('@/views/Layout/index.vue'),
      redirect: '/home',
      children: [
        // 首页
        {
          path: 'home',
          component: () => import('@/views/Home/index.vue'),
          meta: {
            title: '首页'
          }
        },
        // 健康百科
        {
          path: 'article',
          component: () => import('@/views/Article/index.vue'),
          meta: {
            title: '健康百科'
          }
        },
        // 消息通知
        {
          path: 'notify',
          component: () => import('@/views/Notify/index.vue'),
          meta: {
            title: '消息通知'
          }
        },
        // 我的
        {
          path: 'user',
          component: () => import('@/views/User/index.vue'),
          meta: {
            title: '我的'
          }
        }
      ]
    },
    // 家庭档案
    {
      path: '/user/patient',
      component: () => import('@/views/User/patientPage.vue'),
      meta: { title: '家庭档案' }
    },
    // 选择医院页面
    {
      path: '/consult/fast',
      component: () => import('@/views/Consult/ConsultFast.vue'),
      meta: { title: '选择医院' }
    },
    // 选择科室
    {
      path: '/consult/dep',
      component: () => import('@/views/Consult/ConsultDep.vue'),
      meta: { title: '选择科室' }
    },
    // 病情描述
    {
      path: '/consult/illness',
      component: () => import('@/views/Consult/ConsultIllness.vue'),
      meta: { title: '病情描述' }
    },
    // 问诊支付
    {
      path: '/consult/pay',
      component: () => import('@/views/Consult/ConsultPay.vue'),
      meta: { title: '问诊支付' }
    },
    // 问诊室页面
    {
      path: '/room',
      component: () => import('@/views/Room/index.vue'),
      meta: { title: '问诊室' }
    },
    {
      path: '/user/consult',
      component: () => import('@/views/User/ConsultPage.vue'),
      meta: { title: '问诊记录' }
    },
    // 问诊详情页面
    {
      path: '/user/consult/:id',
      component: () => import('@/views/User/ConsultDetail.vue'),
      meta: { title: '问诊详情' }
    },
    // 药品支付页面
    {
      path: '/order/pay',
      component: () => import('@/views/Order/OrderPay.vue'),
      meta: { title: '药品支付' }
    },
    // 药品支付结果页
    {
      path: '/order/pay/result',
      component: () => import('@/views/Order/OrderPayResult.vue'),
      meta: { title: '药品支付结果' }
    },
    // 药品订单详情页
    {
      path: '/order/:id',
      component: () => import('@/views/Order/OrderDetail.vue'),
      meta: { title: '药品订单详情' }
    },
    // 物流详情页
    {
      path: '/order/logistics/:id',
      component: () => import('@/views/Order/OrderLogistics.vue'),
      meta: { title: '物流详情' }
    },
    {
      path: '/login/callback',
      component: () => import('@/views/Login/LoginCallback.vue'),
      meta: { title: 'QQ登录-绑定手机' }
    }
  ]
})

// 全局前置守卫
router.beforeEach((to, from) => {
  // 开启进度条
  NProgress.start()
  const userStore = useUserStore()
  // 没有token并且要去的页面还不是白名单页面，给他跳转到登录页
  const whiteList = ['/login', '/login/callback']
  if (!userStore.user?.token && !whiteList.includes(to.path)) return '/login'
  // 其他情况一律放行
  // return true 或 不 return 都代表直接放行
})

// 全局后置守卫
router.afterEach((to, from) => {
  // console.log(to.meta.title)
  // 设置页面标题
  document.title = `优医问诊-${to.meta.title || ''}`
  NProgress.done()
})

export default router
